package com.test.mylotterysystem.controller;

import com.test.mylotterysystem.comment.utils.MyBeanUtil;
import com.test.mylotterysystem.controller.Param.CreatePrizeParam;
import com.test.mylotterysystem.controller.Param.PageParam;
import com.test.mylotterysystem.controller.Param.PrizeInfo;
import com.test.mylotterysystem.controller.Result.PrizeListResult;
import com.test.mylotterysystem.controller.Result.PrizeListResultDTO;
import com.test.mylotterysystem.service.PrizeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/prize")
@Slf4j
public class PrizeController {
    @Autowired
    private PrizeService prizeService;


    /**
     * 保存图片到本地静态文件中
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping("/savePicture")
    public String savePicture(MultipartFile multipartFile) {
        return prizeService.savePicture(multipartFile);
    }

    /**
     * 创建活动
     *
     * @param createPrizeParam
     * @param multipartFile
     * @return
     */
    @RequestMapping("/create")
    public Long create(@Validated @RequestPart("param") CreatePrizeParam createPrizeParam,
                       @RequestPart("prizePic") MultipartFile multipartFile) {

        log.info("createPrizeParam为：{}", createPrizeParam);
        return prizeService.create(createPrizeParam, multipartFile);
    }


    /**
     * 获取活动列表
     *
     * @param pageParam
     * @return
     */
    @RequestMapping("/getPrizeList")
    public PrizeListResult getPrizeList(@Validated PageParam pageParam) {
        log.info("pageParam为：{}", pageParam);
        PrizeListResultDTO<PrizeInfo> prizeListResultDTO = prizeService.getPrizeList(pageParam);
        return prizeListResultDTO == null ? null : MyBeanUtil.transOfPrizeListResult(prizeListResultDTO);
    }
}
